<?php
declare (strict_types=1);

namespace app\exam\service\admin;

use app\common\service\BaseService;
use app\exam\logic\admin\{QuestionDifficult, QuestionPoint, QuestionTopic, ModuleSpecial};
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 * Class ModuleSpecialTopic
 *
 * @package app\exam\service\admin
 */
class ModuleSpecialTopic
{
    /**
     * @Inject()
     *
     * @var \app\exam\logic\admin\ModuleSpecialTopic
     */
    protected $moduleSpecialTopicLogic;

    /**
     * @Inject()
     *
     * @var QuestionTopic
     */
    protected $questionTopicLogic;

    /**
     * @Inject()
     *
     * @var QuestionDifficult
     */
    protected $questionDifficultLogic;

    /**
     * @Inject()
     *
     * @var QuestionPoint
     */
    protected $questionPointLogic;

    /**
     * 列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/3
     *
     * @param int $specialId
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws \think\db\exception\DbException
     */
    public function index(int $specialId)
    {
        $data = $this->moduleSpecialTopicLogic->getList($specialId);

        // 定义字段
        $keyList = [
            'title'               => ListItem::make('custom')->title('题干'),
            'question_type_text'  => ListItem::make()->title('题型'),
            'question_bank_title' => ListItem::make()->title('所属题库'),
            'difficulty_title'    => ListItem::make()->title('试题难度'),
            'answer_num'          => ListItem::make()->title('作答次数'),
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 获取选项面板配置数据
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/3
     *
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getChooseOptions($moduleId, $specialId)
    {
        $specialLogic = app(ModuleSpecial::class);
        $category     = $specialLogic->whereId($specialId)->value('category');
        $questionType = $this->questionTopicLogic->getObjectiveQuestionTypeOptions();
        $difficulty   = $this->questionDifficultLogic->cascader();
        $point        = $this->questionPointLogic->cascader(0, [['category', '=', $category]]);

        return BaseService::parseToData([
            'options' => [
                'questionType' => $questionType,
                'difficulty'   => $difficulty,
                'point'        => $point
            ]
        ], 1);
    }

    /**
     * 获取题目列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/12/18
     *
     * @return \eduline\admin\page\interfaces\think\Response
     */
    public function getChooseList(int $specialId)
    {
        $map = [];
        // 关键字
        Request::has('title', 'get', true) && $map[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        // 知识点
        $point = Request::get('point/d', 0);
        $point && $map[] = ['point', 'like', '%,' . $point . ',%'];
        // 题型
        if (Request::has('question_type', 'get', true) && Request::get('question_type/d', -1) !== -1) {
            $map[] = ['question_type', '=', Request::get('question_type/d')];
        } else {
            $questionType = $this->questionTopicLogic->getObjectiveQuestionTypeOptions();
            $map[]        = ['question_type', 'in', array_column($questionType, 'value')];
        }
        // 难易度
        $difficulty = Request::get('difficulty/d', 0);
        $difficulty && $map[] = ['point', '=', $difficulty];
        $data = $this->moduleSpecialTopicLogic->getChooseList($specialId, $map);
        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->datas = $data;

        return $list->send();
    }

    /**
     * 新增试题
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/3
     *
     * @param int $specialId
     * @return \think\Response
     * @throws \Exception
     */
    public function create(int $specialId)
    {
        $topics = Request::post('topics/a');
        $this->moduleSpecialTopicLogic->createModuleSpecialTopic($specialId, $topics);

        return BaseService::parseToData([], 1, '新增成功');
    }

    /**
     * 移除试题
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/3
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete(int $specialId, string $id)
    {
        $this->moduleSpecialTopicLogic->deleteModuleSpecialTopic($specialId, $id);

        return BaseService::parseToData([], 1, '移除成功');
    }

    /**
     * 批量移除试题
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/3
     *
     * @param string $ids
     * @return \think\Response
     */
    public function deletes(int $specialId, string $ids)
    {
        $this->moduleSpecialTopicLogic->deleteModuleSpecialTopic($specialId, $ids);

        return BaseService::parseToData([], 1, '批量移除成功');
    }

}